package com.dzt.androidframework.jsoup;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.jsoup.detail.NewsDetailActivity;
import com.dzt.androidframework.jsoup.news.ImageNewContract;
import com.dzt.androidframework.jsoup.news.ImageNewsPresenter;
import com.dzt.androidframework.jsoup.news.adapter.ImageNewsRecycleViewAdapter;
import com.dzt.androidframework.jsoup.news.bean.ImageNews;
import com.dzt.androidframework.jsoup.news.model.RemoteNewsDataSource;
import com.dzt.androidframework.widgets.ScrollChildSwipeRefreshLayout;
import com.wang.avi.AVLoadingIndicatorView;

import org.xutils.view.annotation.ViewInject;

import java.util.List;

import jp.wasabeef.recyclerview.adapters.ScaleInAnimationAdapter;
import jp.wasabeef.recyclerview.animators.SlideInLeftAnimator;

/**
 * 网络爬虫功能示例程序
 * 2017.07.13
 */
public class JsoupActivity extends ActivityTitle implements
		ScrollChildSwipeRefreshLayout.OnRefreshListener,
		ImageNewContract.View {

	@ViewInject(R.id.swipe_refresh_layout)
	private ScrollChildSwipeRefreshLayout swipeRefreshLayout;
	@ViewInject(R.id.recyclerview)
	private RecyclerView recyclerView;
	@ViewInject(R.id.loadingView)
	private AVLoadingIndicatorView loadingView;
	private int currentPage = 1;

	private LinearLayoutManager layoutManager;
	private ImageNewsRecycleViewAdapter adapter;
	private List<ImageNews.DataEntity> imageNewsList;
	private ImageNewsPresenter presenter;

	@Override
	protected int getLayoutId() {
		return R.layout.activity_jsoup;
	}

	@Override
	protected void initParams() {
		setTitleView(R.string.menu_8);

		swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
				android.R.color.holo_green_light,
				android.R.color.holo_orange_light,
				android.R.color.holo_red_light);
		RemoteNewsDataSource dataSource = RemoteNewsDataSource.getInstance();
		presenter = new ImageNewsPresenter(dataSource, this);
		presenter.getImageList(currentPage);

		swipeRefreshLayout.setOnRefreshListener(this);

		layoutManager = new LinearLayoutManager(JsoupActivity.this, LinearLayoutManager.VERTICAL, false);
		recyclerView.setLayoutManager(layoutManager);
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	public void onRefresh() {
		currentPage = 1;
		presenter.getImageList(currentPage);
	}

	@Override
	public void setPresenter(ImageNewContract.Presenter presenter) {

	}

	@Override
	public void addImage(List<ImageNews.DataEntity> imageNewList) {
		if (currentPage == 1) {
			imageNewsList = imageNewList;

			adapter = new ImageNewsRecycleViewAdapter(JsoupActivity.this, imageNewsList);
			recyclerView.setItemAnimator(new SlideInLeftAnimator());
			recyclerView.setAdapter(new ScaleInAnimationAdapter(adapter));
			recyclerView.addOnScrollListener(mOnScrollListener);
			adapter.setOnItemClickListener(new onItemClickListener());
		} else {
			int lastPosition = imageNewsList.size() - 1;
			imageNewsList.addAll(imageNewList);
			adapter.notifyItemRangeInserted(lastPosition, imageNewList.size());
		}
	}

	@Override
	public void showLoading() {
		loadingView.setVisibility(View.VISIBLE);
	}

	@Override
	public void hideLoading() {
		loadingView.setVisibility(View.INVISIBLE);
		swipeRefreshLayout.setRefreshing(false);
	}

	@Override
	public void showLoadFailed() {
		loadingView.setVisibility(View.INVISIBLE);
		Toast.makeText(this, "showLoadFailed", Toast.LENGTH_SHORT).show();
	}

	public class onItemClickListener implements ImageNewsRecycleViewAdapter.OnItemClickListener {
		@Override
		public void onItemClick(View view, int position) {
			//点击进入新闻详情
			Intent intent = new Intent(JsoupActivity.this, NewsDetailActivity.class);
			View transitionView = view.findViewById(R.id.imageView);

			intent.putExtra("image_new_url", imageNewsList.get(position).getUrl());
			intent.putExtra("image_url", imageNewsList.get(position).getCover_thumb_url());
			intent.putExtra("image_new_title", imageNewsList.get(position).getName());
			ActivityOptionsCompat options =
					ActivityOptionsCompat.makeSceneTransitionAnimation(JsoupActivity.this,
							transitionView, getString(R.string.transition_news_img));

			ActivityCompat.startActivity(JsoupActivity.this, intent, options.toBundle());

		}

		@Override
		public void onItemLongClick(View view, int position) {

		}
	}

	private RecyclerView.OnScrollListener mOnScrollListener = new RecyclerView.OnScrollListener() {

		private int lastVisibleItem;

		@Override
		public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
			super.onScrolled(recyclerView, dx, dy);
			lastVisibleItem = layoutManager.findLastVisibleItemPosition();
		}

		@Override
		public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
			super.onScrollStateChanged(recyclerView, newState);
			if (newState == RecyclerView.SCROLL_STATE_IDLE
					&& lastVisibleItem + 1 == adapter.getItemCount()
					&& adapter.isShowFooter()) {
				currentPage++;
				presenter.getImageList(currentPage);
			}
		}
	};
}
